//
//  FieldInfoCell.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/24.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "ZZLayout.h"

#import "FieldInfoCell.h"
#import "FieldInfoCollectionCell.h"

@interface FieldInfoCell ()<UICollectionViewDelegate, UICollectionViewDataSource, ZZLayoutDelegate>

@end
@implementation FieldInfoCell

- (void)awakeFromNib {
    [super awakeFromNib];


    [self initView];
}

- (void)fillCellWithArray:(NSMutableArray <CommonServiceModel *>*)paramArray {
    self.dataArray = @[].mutableCopy;
    self.dataArray = paramArray;
    
    [self.collectionview reloadData];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self pp_clipCorners:UIRectCornerAllCorners radius:8];
}

- (void)initView {
    
    ZZLayout *layout = [[ZZLayout alloc] initWith:ZZLayoutFlowTypeVertical delegate:self];
    self.collectionview.collectionViewLayout = layout;
    self.collectionview.delegate = self;
    self.collectionview.dataSource = self;
    
    //注册cell
    [self.collectionview registerNib:[UINib nibWithNibName:@"FieldInfoCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"FieldInfoCollectionCellID"];
    
    [self.collectionview registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UICollectionElementKindSectionHeader"];

    [self.collectionview registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"UICollectionElementKindSectionFooter"];
    
    self.collectionview.showsHorizontalScrollIndicator = NO;
    self.collectionview.bounces = YES;
//    self.edgesForExtendedLayout = UIRectEdgeNone;
    if (@available(iOS 11.0, *)) {
        self.collectionview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    self.collectionview.backgroundColor = UIColorHex(0xf0f0f0);
}

#pragma mark- 协议方法
//collectionView的协议方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.dataArray.count;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionReusableView *reusableView = nil;
    // 区头
    if (kind == UICollectionElementKindSectionHeader) {
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UICollectionElementKindSectionHeader" forIndexPath:indexPath];
        reusableView = headerView;
    }
    // 区尾
    if (kind == UICollectionElementKindSectionFooter) {
        UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"UICollectionElementKindSectionFooter" forIndexPath:indexPath];
        reusableView = footerView;
    }
    return reusableView;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    FieldInfoCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FieldInfoCollectionCellID" forIndexPath:indexPath];
    cell.itemTitleLab.text = self.dataArray[indexPath.row].itemTitle;
    cell.itemContentLab.text = self.dataArray[indexPath.row].itemContent;
//    int r = rand() % 255;
//    int g = rand() % 255;
//    int b = rand() % 255;
//    cell.backgroundColor = [UIColor colorWithRed:r / 255.0 green:g / 255.0 blue:b / 255.0 alpha:1];
    
    return cell;
    
}

//ZZLyout的流协议方法
- (CGFloat)layout:(ZZLayout *)layout heightForRowAtIndexPath:(NSIndexPath *)indexPath {//返回item的高

    return 30;
}

- (NSInteger)layout:(ZZLayout *)layout columnNumberAtSection:(NSInteger)section {//每个区有几列
    
    return 2;
}

- (UIEdgeInsets)layout:(ZZLayout *)layout insetForSectionAtIndex:(NSInteger)section {//设置每个区的边距
    
    return UIEdgeInsetsMake(5, 5, 5, 5);
}

- (NSInteger)layout:(ZZLayout *)layout lineSpacingForSectionAtIndex:(NSInteger)section {//设置每个区的行间距
    return 1;
}

- (CGFloat)layout:(ZZLayout*)layout interitemSpacingForSectionAtIndex:(NSInteger)section {//设置每个区的列间距
    return 1;
}

- (CGSize)layout:(ZZLayout *)layout referenceSizeForHeaderInSection:(NSInteger)section {//设置区头的高度
    return CGSizeMake(TTScreenWidth, CGFLOAT_MIN);
}

- (CGSize)layout:(ZZLayout *)layout referenceSizeForFooterInSection:(NSInteger)section {//设置区尾的高度
    return CGSizeMake(TTScreenWidth, CGFLOAT_MIN);
}

- (CGFloat)layout:(ZZLayout*)layout spacingWithLastSectionForSectionAtIndex:(NSInteger)section {//本区区头和上个区区尾的间距
    return 0;
}

- (UIColor *)layout:(UICollectionView *)layout colorForSection:(NSInteger)section {//设置不同分区的不同背景颜色
    return UIColorHex(0xf0f0f0);
}

@end
